package com.busi.relevance;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;

public class RelevanceXml  extends HttpServlet {
	
	private final String xmlPath = "/WEB-INF/RelevanceConfig/relev_cfg.xml";
	
	private static Element rootEle;
	
	private static RelevanceXml relevanceXml;
	

	
	public static RelevanceXml getInstance(){
		if(relevanceXml == null){
			relevanceXml = new RelevanceXml();
		}
		return relevanceXml;
	}
	
	ServletConfig config;

	public ServletConfig getConfig() {
		return config;
	}
	public void setConfig(ServletConfig config) {
		this.config = config;
	}
	
	
	public void init(ServletConfig conf)throws ServletException{
		 this.config=conf;
	     String strXmlPath = conf.getServletContext().getRealPath("/") + xmlPath;
	     SAXBuilder sax = new SAXBuilder();
	     try{
	    	 Document doc = sax.build(strXmlPath);
	    	 RelevanceXml.getInstance().rootEle = doc.getRootElement();
	     }catch(Exception e){
	    	 e.printStackTrace();
	     }
	  }
	
	
	public List getSqlListByName(String name){
		List list = new ArrayList();
	    try{
	    	List<Element> listEle = RelevanceXml.getInstance().rootEle.getChildren();
	    	for(Element e : listEle){
	       		if(e.getAttribute("name").getValue().equals(name)){
	    			List<Element> listSql = e.getChildren("hql");
	    			for(Element esql : listSql){
	    				list.add(esql.getTextTrim());
	    			}
	    		}
	    	}
	    	
	    }catch(Exception e){
	    	e.printStackTrace();
	    }
		
	    return list;
		
	}
	
	
	public static void main(String[] args) throws Exception{
		RelevanceXml x = RelevanceXml.getInstance();
		String path = "E:\\xf\\xfweb\\web\\xfweb\\WebContent\\WEB-INF\\RelevanceConfig\\relev_cfg.xml";
		SAXBuilder sax = new SAXBuilder();
   	 	Document doc = sax.build(path);
   	 	x.rootEle = doc.getRootElement();
		x.getSqlListByName("templetId");
	}
	
}
